home *** CD-ROM | disk | FTP | other *** search
- #include <ctype.h>
- #include <LowMem.h>
- #include <NumberFormatting.h>
- #include <QDOffscreen.h>
- #include <string.h>
- #include <string_io.h>
- #include "MacPrint.h"
-
-
-
-
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- // how many pixels should we let free for the menu bar?
- #define kLeaveFreeAtTop 21
-
- #define kGlobalsID ('FDj™')
- #define kFontID ('Fon™')
-
- #define kReinitInterval (120)
-
- #define GLOBALS_ROW *(SInt16*)0x02EC
- #define GLOBALS_COL *(SInt16*)0x02EE
-
- typedef struct MacPrintFont
- {
- UInt32 id;
- UInt16 numChars;
- UInt16 offset;
- UInt8 data[];
- } MacPrintFont, *MacPrintFontPtr;
-
- typedef struct MacPrintGlobals
- {
- UInt32 id;
- UInt8 *pixBase;
- MacPrintFontPtr font;
- UInt32 ticksToReinit;
- UInt32 rowBytes;
- UInt16 depth;
- SInt16 maxrow;
- SInt16 maxcol;
- } MacPrintGlobals;
-
- typedef struct VPrintfInfo
- {
- UInt32 len;
- } VPrintfInfo;
-
- extern void vdprintf(const char *format,va_list arg);
-
- static void MacPrint(char *str);
- static void MacPrint_init(void);
- static void MacPrint_initqd(void);
- static MacPrintFontPtr GetFontPointer(void);
- static UInt32 *GetLookupTable(void);
- #if SMALLER_MACPRINT
- static int MacPrint_vsprintf(char *sbuffer, char *fmt, va_list arg);
- #else
- static int vprintf_write_string(__file_handle handle,unsigned char *buffer,size_t *count,__idle_proc idle_proc);
- #endif
-
- #ifdef __cplusplus
- }
- #endif
-
-
-
-
-
- static UInt8 gPrintTable[256];
- static MacPrintGlobals globals;
-
-
-
-
-
- void MacPrint(char *str)
- {
- MacPrintFontPtr font;
- UInt8 *charPtr;
- UInt8 *destPtr;
- SInt16 charRow;
- SInt16 charsInString;
- SInt16 fontOffset;
- SInt32 i;
- char charByte;
- char theChar;
- SInt32 *longPtr;
- SInt16 *shortPtr;
- UInt32 *nibbleTable;
- SInt16 nibbleIndex;
- SInt16 bitCount;
-
-
- if (globals.id != kGlobalsID)
- MacPrint_init();
-
- if (LMGetTicks() > globals.ticksToReinit)
- MacPrint_initqd();
-
- charsInString = strlen(str);
- if (charsInString == 0)
- return;
-
- font = globals.font;
- fontOffset = font->offset;
-
- nibbleTable = GetLookupTable();
- for (i = 0; i < charsInString; i++) // loop through the string
- {
- theChar = str[i];
-
- if ((theChar == '\n') || (theChar == '\r'))
- {
- GLOBALS_ROW += 1;
- if (GLOBALS_ROW > globals.maxrow)
- GLOBALS_ROW = 0;
-
- GLOBALS_COL = 0;
- }
- else
- {
- charPtr = font->data + 8 * (theChar - fontOffset);
-
- destPtr = globals.pixBase +
- (GLOBALS_COL * globals.depth) +
- (GLOBALS_ROW * globals.rowBytes * 8);
-
- switch (globals.depth)
- {
- case 8: // 256 colors/greys
- charRow = 8;
- while(charRow--)
- {
- charByte = *charPtr++;
-
- longPtr = (SInt32 *)destPtr;
-
- nibbleIndex = ((SInt16)charByte) >> 4;
- *longPtr++ = nibbleTable[nibbleIndex];
-
- nibbleIndex = ((SInt16)charByte) & 0x0F;
- *longPtr = nibbleTable[nibbleIndex];
- destPtr += globals.rowBytes;
- }
-
- longPtr = (SInt32 *)destPtr;
-
- *longPtr++ = -1L;
- *longPtr++ = -1L;
- break;
-
- case 16: // thousands
- charRow = 8;
- while(charRow--)
- {
- charByte = *charPtr++;
-
- shortPtr = (SInt16 *)destPtr;
-
- *shortPtr++ = ((charByte & (1 << 7)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 6)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 5)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 4)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 3)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 2)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 1)) != 0) ? 0 : -1;
- *shortPtr++ = ((charByte & (1 << 0)) != 0) ? 0 : -1;
-
- destPtr += globals.rowBytes;
- }
-
- longPtr = (SInt32 *)destPtr;
- *longPtr++ = 0L; *longPtr++ = 0L; *longPtr++ = 0L; *longPtr++ = 0L;
- break;
-
-
- case 32: // millions
- charRow = 8;
- while(charRow--)
- {
- charByte = *charPtr++;
-
- longPtr = (SInt32 *)destPtr;
-
- for (bitCount = 8; bitCount > 0; bitCount --)
- *longPtr++ =
- ((charByte & (1 << bitCount)) != 0) ? 0L : -1L;
-
-
- destPtr += globals.rowBytes;
- }
-
- longPtr = (SInt32 *)destPtr;
- *longPtr++ = 0L; *longPtr++ = 0L; *longPtr++ = 0L; *longPtr++ = 0L;
- *longPtr++ = 0L; *longPtr++ = 0L; *longPtr++ = 0L; *longPtr++ = 0L;
- break;
- }
-
- GLOBALS_COL += 1;
- if (GLOBALS_COL > globals.maxcol)
- {
- GLOBALS_ROW += 1;
- if (GLOBALS_ROW > globals.maxrow)
- GLOBALS_ROW = 0;
-
- GLOBALS_COL = 0;
- }
- }
- }
- }
-
-
-
-
-
- void MacPrint_printf(const char *format,...)
- {
- va_list arg;
-
-
- va_start(arg,format);
- MacPrint_vprintf(format,arg);
- va_end(arg);
- }
-
-
-
-
-
- void MacPrint_vprintf(const char *format,va_list arg)
- {
- if (*(SInt8*)0x910 <= 0)
- {
- #if SMALLER_MACPRINT
- char buffer[512];
-
- MacPrint_vsprintf(buffer,(char*)format,arg);
- MacPrint(buffer);
- #else
- char text[sizeof(VPrintfInfo) + 513];
- VPrintfInfo *info = (VPrintfInfo*)&text[0];
- SInt32 len;
- FILE file;
-
-
- info->len = 0;
-
- __open_string_file(&file,&text[sizeof(VPrintfInfo)],(sizeof(text) - 1) - sizeof(VPrintfInfo),__writing);
- file.write_proc = vprintf_write_string;
-
- len = vfprintf(&file,format,arg);
- len -= info->len;
-
- if (len > 0)
- {
- text[sizeof(VPrintfInfo) + len] = '\0';
- MacPrint(&text[sizeof(VPrintfInfo)]);
- }
- #endif
- }
-
- #undef vdprintf
- vdprintf(format,arg);
- }
-
-
-
-
-
- void MacPrint_printmem(const void *data,size_t len)
- {
- char bin2hex[] = "0123456789ABCDEF";
- char ascii[17],hex[41],*hstr,*astr;
- Boolean partial = (len < 16);
- UInt32 spaces;
- UInt8 byte;
-
-
- // Print header line.
- MacPrint_printf("Displaying %lu bytes from %08lX\r",len,data);
-
- // Print full lines.
- for (ascii[16] = '\0';len >= 16;len -= 16,(UInt8*)data += 16)
- {
- for (UInt32 index = 0;index < 16;index++)
- ascii[index] = gPrintTable[*((UInt8*)data + index)];
-
- MacPrint_printf("%08lX %04X %04X %04X %04X %04X %04X %04X %04X '%s'\r",data,*((UInt16*)data + 0),
- *((UInt16*)data + 1),*((UInt16*)data + 2),*((UInt16*)data + 3),*((UInt16*)data + 4),
- *((UInt16*)data + 5),*((UInt16*)data + 6),*((UInt16*)data + 7),ascii);
- }
-
- // Print remainder/partial line.
- if (len > 0)
- {
- spaces = 0;
- hstr = &hex[0];
- astr = &ascii[0];
- for (UInt32 index = 0;index < len;index++)
- {
- spaces = 0;
- byte = *((UInt8*)data + index);
- *astr++ = gPrintTable[byte];
- *hstr++ = bin2hex[byte >> 4];
- *hstr++ = bin2hex[byte & 15];
-
- if ((index & 7) == 7)
- {
- *hstr++ = ' ';
- spaces += 1;
- }
-
- if ((index & 1) == 1)
- {
- *hstr++ = ' ';
- spaces += 1;
- }
- }
-
- while(spaces < 2)
- {
- *hstr++ = ' ';
- spaces += 1;
- }
-
- *hstr = '\0';
- *astr = '\0';
-
- MacPrint_printf("%08lX %s%*s'%s'\r",data,hex,partial ? 0 : (42 - strlen(hex)),"",ascii);
- }
- }
-
-
-
-
-
- void MacPrint_fprintf(const char *log,const char *format,...)
- {
- #pragma unused(log)
- va_list arg;
-
-
- va_start(arg,format);
- MacPrint_vprintf(format,arg);
- va_end(arg);
- }
-
-
-
-
-
- void MacPrint_vfprintf(const char *log,const char *format,va_list arg)
- {
- #pragma unused(log)
- MacPrint_vprintf(format,arg);
- }
-
-
-
-
-
- void MacPrint_fprintmem(const char *log,const void *data,size_t len)
- {
- #pragma unused(log)
- MacPrint_printmem(data,len);
- }
-
-
- #if 0
- #pragma mark -
- #endif
-
-
- static void MacPrint_init(void)
- {
- #if SMALLER_MACPRINT
- for (UInt32 index = 0;index < 256;index++)
- gPrintTable[index] = (index & 0x80) ? '.' : index;
- #else
- for (UInt32 index = 0;index < 256;index++)
- gPrintTable[index] = (index & 0x80) ? '.' : isprint(index) ? index : '.';
- #endif
-
- globals.id = 0;
- globals.font = GetFontPointer();
-
- MacPrint_initqd();
-
- if (*(UInt16*)0x02EA != 0x4D50)
- {
- GLOBALS_ROW = 0;
- GLOBALS_COL = 0;
- *(UInt16*)0x02EA = 0x4D50;
- }
-
- globals.id = kGlobalsID; // these globals are valid
- }
-
-
-
-
-
- static void MacPrint_initqd(void)
- {
- GDHandle gdh;
- PixMapHandle pmh;
-
-
- gdh = GetMainDevice();
- pmh = (**gdh).gdPMap;
-
- globals.pixBase = (UInt8*)GetPixBaseAddr(pmh);
- globals.depth = (**pmh).pixelSize;
- globals.rowBytes = (0x3fff & (**pmh).rowBytes);
- globals.maxrow = ((**pmh).bounds.bottom - (**pmh).bounds.top - kLeaveFreeAtTop) / 8 - 1;
- globals.maxcol = ((**pmh).bounds.right - (**pmh).bounds.left) / 8;
- globals.pixBase += (globals.rowBytes * kLeaveFreeAtTop);
-
- globals.ticksToReinit = LMGetTicks() + kReinitInterval;
-
- if (GLOBALS_COL > globals.maxcol)
- {
- GLOBALS_ROW += 1;
- if (GLOBALS_ROW > globals.maxrow)
- GLOBALS_ROW = 0;
-
- GLOBALS_COL = 0;
- }
-
- if (GLOBALS_ROW > globals.maxrow)
- GLOBALS_ROW = GLOBALS_COL = 0;
- }
-
-
-
-
-
- static MacPrintFontPtr GetFontPointer(void)
- {
- static UInt16 theFont[] = {
- 0x466F, 0x6EAA, 0x005A, 0x0020, // id1, id2, numChars, offset
- 0x0000, 0x0000, 0x0000, 0x0000, // blank
- 0x1818, 0x1818, 0x1800, 0x1800, // !
- 0x1414, 0x0000, 0x0000, 0x0000, // "
- 0x2424, 0x7E24, 0x7E24, 0x2400, // #
- 0x183E, 0x583C, 0x1A7C, 0x1800, // $
- 0x43A6, 0x4C18, 0x3265, 0xC200, // %
- 0x386C, 0x306A, 0x6C6A, 0x3000, // &
- 0x1818, 0x0800, 0x0000, 0x0000, // '
- 0x1830, 0x3030, 0x3030, 0x1800, // (
- 0x180C, 0x0C0C, 0x0C0C, 0x1800, // )
- 0x185A, 0x3C18, 0x3C5A, 0x1800, // *
- 0x0018, 0x187E, 0x1818, 0x0000, // +
- 0x0000, 0x0000, 0x1818, 0x0810, // ,
- 0x0000, 0x003C, 0x0000, 0x0000, // -
- 0x0000, 0x0000, 0x0018, 0x1800, // .
- 0x0006, 0x0C18, 0x3060, 0x0000, // /
- 0x3C66, 0x6E76, 0x6666, 0x3C00, // 0
- 0x0C3C, 0x0C0C, 0x0C0C, 0x0C00, // 1
- 0x3C66, 0x060C, 0x1830, 0x7E00, // 2
- 0x3C66, 0x060C, 0x0666, 0x3C00, // 3
- 0x0C1C, 0x3C6C, 0x7E0C, 0x0C00, // 4
- 0x7E60, 0x7C06, 0x0666, 0x3C00, // 5
- 0x3C60, 0x607C, 0x6666, 0x3C00, // 6
- 0x7E0C, 0x0C18, 0x1830, 0x3000, // 7
- 0x3C66, 0x663C, 0x6666, 0x3C00, // 8
- 0x3C66, 0x663E, 0x0606, 0x3C00, // 9
- 0x0000, 0x1818, 0x0018, 0x1800, // :
- 0x0018, 0x1800, 0x1818, 0x0810, // ;
- 0x0C18, 0x3060, 0x3018, 0x0C00, // <
- 0x0000, 0x003C, 0x003C, 0x0000, // =
- 0x3018, 0x0C06, 0x0C18, 0x3000, // >
- 0x3C66, 0x060C, 0x1800, 0x1800, // ?
- 0x3E41, 0x5D55, 0x5E40, 0x3E00, // @
- 0x3C66, 0x667E, 0x6666, 0x6600, // A
- 0x7C66, 0x667C, 0x6666, 0x7C00, // B
- 0x3C66, 0x6060, 0x6066, 0x3C00, // C
- 0x7C66, 0x6666, 0x6666, 0x7C00, // D
- 0x7E60, 0x6078, 0x6060, 0x7E00, // E
- 0x7E60, 0x6078, 0x6060, 0x6000, // F
- 0x3C66, 0x606E, 0x6666, 0x3C00, // G
- 0x6666, 0x667E, 0x6666, 0x6600, // H
- 0x3C18, 0x1818, 0x1818, 0x3C00, // I
- 0x7E06, 0x0606, 0x0666, 0x3C00, // J
- 0x6666, 0x6C78, 0x6C66, 0x6600, // K
- 0x6060, 0x6060, 0x6060, 0x7E00, // L
- 0x6276, 0x6A62, 0x6262, 0x6200, // M
- 0x6272, 0x6A66, 0x6262, 0x6200, // N
- 0x3C66, 0x6666, 0x6666, 0x3C00, // O
- 0x7C66, 0x667C, 0x6060, 0x6000, // P
- 0x3C66, 0x6666, 0x6E6E, 0x3E00, // Q
- 0x7C66, 0x667C, 0x6666, 0x6600, // R
- 0x3C66, 0x603C, 0x0666, 0x3C00, // S
- 0x7E18, 0x1818, 0x1818, 0x1800, // T
- 0x6666, 0x6666, 0x6666, 0x3E00, // U
- 0x6666, 0x6666, 0x6624, 0x1800, // V
- 0x6262, 0x6262, 0x6A76, 0x6200, // W
- 0x6666, 0x2418, 0x2466, 0x6600, // X
- 0x6666, 0x2418, 0x1818, 0x1800, // Y
- 0x7E06, 0x0C18, 0x3060, 0x7E00, // Z
- 0x1C10, 0x1010, 0x1010, 0x1C00, // [
- 0x4060, 0x3018, 0x0C06, 0x0200, // '\'
- 0x3808, 0x0808, 0x0808, 0x3800, // ]
- 0x1C36, 0x6300, 0x0000, 0x0000, // ^
- 0x0000, 0x0000, 0x0000, 0xFF00, // _
- 0x3018, 0x0C00, 0x0000, 0x0000, // `
- 0x3C66, 0x667E, 0x6666, 0x6600, // A
- 0x7C66, 0x667C, 0x6666, 0x7C00, // B
- 0x3C66, 0x6060, 0x6066, 0x3C00, // C
- 0x7C66, 0x6666, 0x6666, 0x7C00, // D
- 0x7E60, 0x6078, 0x6060, 0x7E00, // E
- 0x7E60, 0x6078, 0x6060, 0x6000, // F
- 0x3C66, 0x606E, 0x6666, 0x3C00, // G
- 0x6666, 0x667E, 0x6666, 0x6600, // H
- 0x3C18, 0x1818, 0x1818, 0x3C00, // I
- 0x7E06, 0x0606, 0x0666, 0x3C00, // J
- 0x6666, 0x6C78, 0x6C66, 0x6600, // K
- 0x6060, 0x6060, 0x6060, 0x7E00, // L
- 0x6276, 0x6A62, 0x6262, 0x6200, // M
- 0x6272, 0x6A66, 0x6262, 0x6200, // N
- 0x3C66, 0x6666, 0x6666, 0x3C00, // O
- 0x7C66, 0x667C, 0x6060, 0x6000, // P
- 0x3C66, 0x6666, 0x6E6E, 0x3E00, // Q
- 0x7C66, 0x667C, 0x6666, 0x6600, // R
- 0x3C66, 0x603C, 0x0666, 0x3C00, // S
- 0x7E18, 0x1818, 0x1818, 0x1800, // T
- 0x6666, 0x6666, 0x6666, 0x3E00, // U
- 0x6666, 0x6666, 0x6624, 0x1800, // V
- 0x6262, 0x6262, 0x6A76, 0x6200, // W
- 0x6666, 0x2418, 0x2466, 0x6600, // X
- 0x6666, 0x2418, 0x1818, 0x1800, // Y
- 0x7E06, 0x0C18, 0x3060, 0x7E00, // Z
- };
-
- return (MacPrintFontPtr)theFont;
- }
-
-
-
-
-
- UInt32 *GetLookupTable(void)
- {
- static UInt32 table[] = {
- ~0x00000000,
- ~0x000000FF,
- ~0x0000FF00,
- ~0x0000FFFF,
- ~0x00FF0000,
- ~0x00FF00FF,
- ~0x00FFFF00,
- ~0x00FFFFFF,
- ~0xFF000000,
- ~0xFF0000FF,
- ~0xFF00FF00,
- ~0xFF00FFFF,
- ~0xFFFF0000,
- ~0xFFFF00FF,
- ~0xFFFFFF00,
- ~0xFFFFFFFF
- };
-
-
- return table;
- }
-
-
-
-
- #if !SMALLER_MACPRINT
- int vprintf_write_string(__file_handle handle,unsigned char *buffer,size_t *count,__idle_proc idle_proc)
- {
- VPrintfInfo *info = (VPrintfInfo*)((UInt32)buffer - sizeof(VPrintfInfo));
-
-
- if (*count > 0)
- {
- buffer[*count] = '\0';
- MacPrint((char*)buffer);
- }
-
- info->len += *count;
- return(__no_io_error);
- }
- #endif
-
-
-
-
- #if SMALLER_MACPRINT
- static struct format
- {
- unsigned leftJustify : 1;
- unsigned forceSign : 1;
- unsigned altForm : 1;
- unsigned zeroPad : 1;
- unsigned havePrecision : 1;
- unsigned hSize : 1;
- unsigned lSize : 1;
- unsigned LSize : 1;
- char sign;
- char exponent;
- int fieldWidth;
- int precision;
- } default_format;
-
- struct decrec
- {
- char sgn;
- short exp;
- // char sig[SIGDIGLEN];
- short pad;
- // following fields aren't used by SANE
- short min;
- short dot;
- short max;
- };
-
- #define BUFLEN 512
-
- int MacPrint_vsprintf(char *sbuffer, char *fmt, va_list arg)
- {
- register int c, i, j, nwritten = 0;
- register unsigned long n;
- register char *s;
- char buf[BUFLEN], *digits, *t;
- struct format F;
- struct decrec D;
-
- for (c = *fmt; c; c = *++fmt)
- {
- if (c != '%') goto copy1;
- F = default_format;
-
- // decode flags
-
- for (;;)
- {
- c = *++fmt;
- if (c == '-') F.leftJustify = true;
- else if (c == '+') F.forceSign = true;
- else if (c == ' ') F.sign = ' ';
- else if (c == '#') F.altForm = true;
- else if (c == '0') F.zeroPad = true;
- else break;
- }
-
- // decode field width
-
- if (c == '*')
- {
- if ((F.fieldWidth = va_arg(arg, int)) < 0)
- {
- F.leftJustify = true;
- F.fieldWidth = -F.fieldWidth;
- }
- c = *++fmt;
- }
- else
- {
- for (; c >= '0' && c <= '9'; c = *++fmt)
- F.fieldWidth = (10 * F.fieldWidth) + (c - '0');
- }
-
- // decode precision
-
- if (c == '.')
- {
- if ((c = *++fmt) == '*')
- { F.precision = va_arg(arg, int); c = *++fmt; }
- else for (; c >= '0' && c <= '9'; c = *++fmt)
- F.precision = (10 * F.precision) + (c - '0');
- if (F.precision >= 0) F.havePrecision = true;
- }
-
- // perform appropriate conversion
-
- s = &buf[BUFLEN];
- if (F.leftJustify) F.zeroPad = false;
-
- conv: switch (c)
- {
- case 'h' : F.hSize = true; c = *++fmt; goto conv;
- case 'l' : F.lSize = true; c = *++fmt; goto conv;
- case 'L' : F.LSize = true; c = *++fmt; goto conv;
- case 'd' :
- case 'i' : if (F.lSize) n = va_arg(arg, long);
- else n = va_arg(arg, int);
- if (F.hSize) n = (short) n;
- if ((long) n < 0) { n = -n; F.sign = '-'; }
- else if (F.forceSign) F.sign = '+';
- goto decimal;
- case 'u' : if (F.lSize) n = va_arg(arg, unsigned long);
- else n = va_arg(arg, unsigned int);
- if (F.hSize) n = (unsigned short) n;
- F.sign = 0;
- goto decimal;
- decimal: if (!F.havePrecision)
- {
- if (F.zeroPad)
- {
- F.precision = F.fieldWidth;
- if (F.sign) --F.precision;
- }
- if (F.precision < 1) F.precision = 1;
- }
- for (i = 0; n; n /= 10, i++) *--s = n % 10 + '0';
- for (; i < F.precision; i++) *--s = '0';
- if (F.sign) { *--s = F.sign; i++; }
- break;
-
- case 'o' : if (F.lSize) n = va_arg(arg, unsigned long);
- else n = va_arg(arg, unsigned int);
- if (F.hSize) n = (unsigned short) n;
- if (!F.havePrecision)
- {
- if (F.zeroPad) F.precision = F.fieldWidth;
- if (F.precision < 1) F.precision = 1;
- }
- for (i = 0; n; n /= 8, i++) *--s = n % 8 + '0';
- if (F.altForm && i && *s != '0') { *--s = '0'; i++; }
- for (; i < F.precision; i++) *--s = '0';
- break;
-
- case 'p' : F.havePrecision = F.lSize = true;
- F.precision = 8;
- case 'X' : digits = "0123456789ABCDEF";
- goto hexadecimal;
- case 'x' : digits = "0123456789abcdef";
- hexadecimal:if (F.lSize) n = va_arg(arg, unsigned long);
- else n = va_arg(arg, unsigned int);
- if (F.hSize) n = (unsigned short) n;
- if (!F.havePrecision)
- {
- if (F.zeroPad)
- {
- F.precision = F.fieldWidth;
- if (F.altForm) F.precision -= 2;
- }
- if (F.precision < 1) F.precision = 1;
- }
- for (i = 0; n; n /= 16, i++) *--s = digits[n % 16];
- for (; i < F.precision; i++) *--s = '0';
- if (F.altForm) { *--s = c; *--s = '0'; i += 2; }
- break;
-
- case 'c' : *--s = va_arg(arg, int); i = 1; break;
-
- case 's' : s = va_arg(arg, char *);
- if (F.altForm)
- {
- i = (unsigned char) *s++;
- if (F.havePrecision && i > F.precision) i = F.precision;
- }
- else
- {
- i = strlen(s);
- if (F.havePrecision && i > F.precision) i = F.precision;
- }
- break;
-
- case 'n' : s = (char*)va_arg(arg, void *);
- if (F.hSize) * (short *) s = nwritten;
- else if (F.lSize) * (long *) s = nwritten;
- else * (int *) s = nwritten;
- continue;
-
- // oops - unknown conversion, abort
-
- case 'M': case 'N': case 'O': case 'P': case 'Q':
- case 'R': case 'S': case 'T': case 'U': case 'V':
- // (extra cases force this to be an indexed switch)
- default: goto done;
-
- case '%' :
- copy1 : *sbuffer++ = c; ++nwritten; continue;
- }
-
- // pad on the left
-
- if (i < F.fieldWidth && !F.leftJustify)
- do { *sbuffer++ = ' '; ++nwritten; } while (i < --F.fieldWidth);
-
- // write the converted result
-
- for (j=0; j<i; j++) *sbuffer++ = *s++;
- nwritten += i;
-
- // pad on the right
-
- for (; i < F.fieldWidth; i++)
- { *sbuffer++ = ' '; ++nwritten; }
- }
-
- done: return(nwritten);
- }
- #endif
-